/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pnt.message;

import java.net.InetSocketAddress;

import pnt.message.objects.*;

/**
 *
 * @author andrew
 */
public class Request_Register extends Request {

    boolean userauthrequired;
    AuthorizationType authorization;
    User[] registeredwith;
    User from;
    User to;
    User enduser;
    String applicationname;
    int instance;
    InetSocketAddress[] privateendpoints;
    ExtEndpoint[] extendpoints;
    int expires;

    public Request_Register(int transactionNumber,
                    boolean userauthrequired, 
                    AuthorizationType authorization, 
                    User[] registeredwith, 
                    User from, 
                    User to, 
                    User enduser, 
                    String applicationName, 
                    int instance, 
                    InetSocketAddress[] privateendpoints, 
                    ExtEndpoint[] extendpoints, 
                    int expires) {
        super(transactionNumber, Request.RequestType.REGISTER);
        this.userauthrequired = userauthrequired;
        this.authorization = authorization;
        this.registeredwith = registeredwith;
        this.from = from;
        this.to = to;
        this.enduser = enduser;
        this.applicationname = applicationName;
        this.instance = instance;
        this.privateendpoints = privateendpoints;
        this.extendpoints = extendpoints;
        this.expires = expires;
    }
    
    @Override
    public String toString(String endChar, String term) {
        
        String s  = "REGISTER * PNT/1.0" + endChar +
                    "Transaction: " + getTransactionNumber() + endChar +
                    "User-Auth-Required: " + ( userauthrequired ? "Yes" : "No" ) + endChar +
                    "Authorization: " + authorization.toString() + endChar +
                    "Registered-With: " + Utils.formatUsers(registeredwith) + endChar +
                    "From: " + from.toString() + endChar +
                    "To: " + to.toString() + endChar +
                    "End-User: " + enduser.toString() + endChar +
                    "Application: " + applicationname + endChar +
                    "Instance: " + instance + endChar +
                    "Private-endpoints: " + Utils.formatSocketAddresses(privateendpoints) + endChar;
               s += "Ext-endpoints: ";
                        int elen = (extendpoints == null) ? 0 : extendpoints.length;
                        for (int n = 0; n < elen; n++){
                            s += extendpoints[n].toString() + 
                                 ((n+1 == elen)? endChar : ",");
                        }
               s += "Expires: " + expires + term;
             
        return s;
    }

    @Override
    public byte[] toBytes() {
        return toString().getBytes();
    }

    public enum AuthorizationType{
    
        ANY, REGISTERED_USERS, AUTHENTICATED_USERS, REGISTERED_BY_SELF;
    }

	public boolean isUserauthrequired() {
		return userauthrequired;
	}

	public AuthorizationType getAuthorization() {
		return authorization;
	}

	public User[] getRegisteredwith() {
		return registeredwith;
	}

	public User getFrom() {
		return from;
	}

	public User getTo() {
		return to;
	}

	public User getEnduser() {
		return enduser;
	}

	public String getApplicationname() {
		return applicationname;
	}

	public int getInstance() {
		return instance;
	}

	public InetSocketAddress[] getPrivateendpoints() {
		return privateendpoints;
	}

	public ExtEndpoint[] getExtendpoints() {
		return extendpoints;
	}

	public int getExpires() {
		return expires;
	}
}
